#!/bin/sh

# Post Install Script

# modify the apache config file, taking exceptions for itools and os x server users

hpath=/usr/local/Apache2/conf
hfile="${hpath}/httpd.conf"

if [ -f "${hfile}" ]; then

	# remove the lasso additions to the httpd.conf file if they are present
	while [ $? = 0 ];
	do
		ed - "${hfile}" > /dev/null 2>&1 << 'LASSOREMOVE' > /dev/null 2>&1
/#Begin: Added by Lasso [0-9]/,/#End: Added by Lasso [0-9]/d
w
q
LASSOREMOVE
	done

	# if we don't have an include path, we need to add one
	if ! (cat "${hfile}" | grep -q '^Include.*lasso8.conf$')
	then

		#echo not found
		cp "${hfile}" "${hfile}.lassobackup"

		ed - "${hfile}" > /dev/null 2>&1 << 'LASSOADD' > /dev/null 2>&1
$
a
#Begin: Added by Lasso 8 Installer
Include conf/lasso8.conf
#End: Added by Lasso 8 Installer
.
w
q
LASSOADD

	fi
fi


# set permissions

THEUID=`ps -acxouid,command | grep Finder | sed -e 's/[ ]*\([^ ]*\).*/\1/g'`

/usr/local/Apache2/bin/apachectl restart > /dev/null 2>&1
sleep 1

if [ ! -f /private/tmp/ls8install ]; then
	if [ "$COMMAND_LINE_INSTALL" = 1 ] || [ -f /private/tmp/lp8windowopen ]; then
		echo "Command line install, not opening window."
	else
		for IP in `ifconfig -u -a inet 2> /dev/null | sed -n 's/.*inet \([0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\)/\1/pg' | awk '{ print $1 }'`; do

			if [[ `curl -I http://$IP/ServerAdmin.LassoApp --silent` = *Server:\ Lasso*8* ]];
			then
				echo "Opening default browser for user #${THEUID}"
				sudo -u "#${THEUID}" open "http://${IP}/ServerAdmin.LassoApp" > /dev/null 2>&1
				touch /private/tmp/lp8windowopen
				break;
			fi

		done

	fi
fi

echo ""
